package goods

import (
	"errors"
	"github.com/gin-gonic/gin"
	"higame/routes/user"
	"strings"
)

func validatorTrade(ctx *gin.Context) (*Schema, error) {
	v := New()
	if err := ctx.ShouldBind(v); err != nil {
		errText := err.Error()
		var msgList [] string
		if strings.Contains(errText, "'Title' failed on the 'required' tag") {
			msgList = append(msgList, "商品标题不能为空")
		}

		if strings.Contains(errText, "'Price' failed on the 'required' tag") {
			msgList = append(msgList, "价格不能为空")
		}

		if strings.Contains(errText, "'MainImage' failed on the 'required' tag") {
			msgList = append(msgList, "主图网址不能为空")
		}

		if strings.Contains(errText, "'SourceUrl' failed on the 'required' tag") {
			msgList = append(msgList, "商品来源网址不能为空")
		}

		if len(msgList) > 0 {
			return nil, errors.New(strings.Join(msgList, "，") + "。")
		}
	}

	return v, nil
}

func getAddress(userItem *user.Schema, addressId string) (user.AddressSchema, error) {
	for _, item := range userItem.Address {
		if item.UUID == addressId {
			return item, nil
		}
	}

	return user.AddressSchema{}, errors.New("没有找到地址")
}
